package org.mcs.controlador.mantenimiento;   
import java.io.FileNotFoundException;
import java.io.FileOutputStream; 
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.List; 

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;  
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; 
import com.google.gson.Gson; 
import com.google.gson.GsonBuilder;

import emp.entidades.EntBanco;
import emp.entidades.EntEmpresa;
import emp.jsf.servicio.BancoService;
import emp.jsf.servicio.EmpresaService;
   

@Controller  
public class BancoController {

	@Autowired
	BancoService bancoService;
	  
	@Autowired
	EmpresaService empresaService;
	
	public static String URL_PAGINA="banco-mnt"; 
	
	  @RequestMapping(method=RequestMethod.GET, value="/banco")
	   public ModelAndView carga() {
		  EntBanco banco=new EntBanco();   
		  banco.setEntEmpresa(new EntEmpresa()); 
		  ModelAndView mov= new ModelAndView(URL_PAGINA, "command",banco); 
		  return mov;
	   }
	   
	  
	  @RequestMapping(method=RequestMethod.GET, value="banco/lista") 
	   public  @ResponseBody   void  lista(HttpServletResponse response  ) throws UnsupportedEncodingException, FileNotFoundException, IOException { 
		  
		  List<EntBanco> listaBanco=bancoService.listarBancos();
		  for(EntBanco u:listaBanco){ 
			  u.setEntEmpresa(null);
		  } 
		  
		  	Gson gson = new Gson(); 
	  		String resultado=gson.toJson(listaBanco);
	  		
	  		OutputStream os = response.getOutputStream();
			byte[] content =resultado.getBytes();
			os.write(content);
			response.setContentType("text/html;charset=iso-8859-1");
			response.setContentLength(content.length);
 
	   }
 
 
	  
	  @RequestMapping(value = "banco/nuevo", method = RequestMethod.POST)
	   public ModelAndView nuevo(@ModelAttribute("SpringWeb")EntBanco banco, ModelMap model, final RedirectAttributes redirectAttributes) {
		  EntBanco bancoNuevo=new EntBanco();   
		  bancoNuevo.setEntEmpresa(new EntEmpresa()); 
		  ModelAndView mov=new ModelAndView(URL_PAGINA, "command",bancoNuevo ); 
		  try { 
			  if(banco!=null && banco.getNombre()!=null){ 
				  		  EntEmpresa e=empresaService.listarEmpresas().get(0);//reemplazar por otra logica
				  		  banco.setEntEmpresa(e);
						  EntBanco bancoGrabado=bancoService.grabarBanco(banco);
					      if(bancoGrabado.getId()==null){ 
					    	  mov.addObject( "messageerror","Error al grabar. Vuelva a intentarlo");
					      }
			  }
			} catch (Exception e) { 
				mov.addObject(  "messageerror","Error generico. Si el problema persiste contacte a soporte"); 
				e.printStackTrace();
			}finally{ 
				return mov;
			}
	   }
	  
	  
	  @RequestMapping(value = "banco/modifica", method = RequestMethod.POST)
	   public ModelAndView modifica(@ModelAttribute("SpringWeb")EntBanco banco, ModelMap model, final RedirectAttributes redirectAttributes) {
		  EntBanco bancoNuevo=new EntBanco();   
		  bancoNuevo.setEntEmpresa(new EntEmpresa()); 
		  ModelAndView mov=new ModelAndView(URL_PAGINA, "command",bancoNuevo);

		  try {
			  
			  if(banco!=null && banco.getNombre()!=null){
				  		  EntEmpresa e=empresaService.listarEmpresas().get(0);//reemplazar por otra logica
				  		  banco.setEntEmpresa(e);
						  EntBanco bancoGrabado=bancoService.actualizarBanco(banco);
					      if(bancoGrabado.getId()==null){ 
					    	  mov.addObject( "messageerror","Error al grabar. Vuelva a intentarlo");
					      }
					       
			  }
			} catch (Exception e) { 
				mov.addObject(  "messageerror","Error generico. Si el problema persiste contacte a soporte"); 
				e.printStackTrace();
			}finally{ 
				return mov;
			}
	   }
	  
	 
	  @RequestMapping(method=RequestMethod.POST, value="banco/eliminar") 
	   public  ModelAndView eliminar(@ModelAttribute("id")String id, ModelMap model, final RedirectAttributes redirectAttributes) {
		  ModelAndView mov=new ModelAndView(URL_PAGINA, "command",new EntBanco() );
		  try {  
			  bancoService.eliminar(id);
		  	} catch (Exception e) { 
				mov.addObject(  "messageerror","Error generico. Si el problema persiste contacte a soporte");
				e.printStackTrace();
			}
	  		return mov; 
	   }
	 
}
